package com.linkage.educloud.ah.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.linkage.educloud.ah.R;

/**
 * Created by Mr'Tang on 2018/9/5.
 * 自定义小标题栏
 */

public class MyMore extends LinearLayout {
    private TextView mymore_desc, mymore_more;
    private View mymore_view, bottom_line;

    public MyMore(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.view_mymore, this, true);
        mymore_desc = findViewById(R.id.mymore_desc);
        mymore_more = findViewById(R.id.mymore_more);
        mymore_view = findViewById(R.id.mymore_view);
        bottom_line = findViewById(R.id.bottom_line);
        TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.MyMore);
        if (attributes != null) {
            String leftText = attributes.getString(R.styleable.MyMore_text_left);
            int leftDrawable = attributes.getResourceId(R.styleable.MyMore_drawable_left, 0);
            int rightDrawable = attributes.getResourceId(R.styleable.MyMore_drawable_right, 0);
            if (leftDrawable != 0) {
                Drawable drawable_left = getResources().getDrawable(leftDrawable);
                drawable_left.setBounds(0, 0, drawable_left.getMinimumWidth(), drawable_left.getMinimumHeight());
                mymore_desc.setCompoundDrawables(drawable_left, null, null, null);
            }
            if (rightDrawable != 0) {
                Drawable drawable_right = getResources().getDrawable(rightDrawable);
                drawable_right.setBounds(0, 0, drawable_right.getMinimumWidth(), drawable_right.getMinimumHeight());
                mymore_more.setCompoundDrawables(null, null, drawable_right, null);
            }
            if (!TextUtils.isEmpty(leftText)) {
                mymore_desc.setText(leftText);
            }
            String rightText = attributes.getString(R.styleable.MyMore_text_right);
            if (!TextUtils.isEmpty(rightText)) {
                mymore_more.setText(rightText);
            }
            boolean leftViewVisible = attributes.getBoolean(R.styleable.MyMore_left_view_visible, false);
            boolean buttomLineVisible = attributes.getBoolean(R.styleable.MyMore_bottom_line_visible, false);
            if (buttomLineVisible) {
                bottom_line.setVisibility(VISIBLE);
            } else {
                bottom_line.setVisibility(GONE);
            }
            if (leftViewVisible) {
                mymore_view.setVisibility(View.VISIBLE);
            } else {
                mymore_view.setVisibility(View.INVISIBLE);
            }
            attributes.recycle();
        }
    }

    public void setRightText(String text) {
        mymore_more.setText(text);
    }

    public void setLeftText(String text) {
        mymore_desc.setText(text);
    }

    public void setMoreClickListener(OnClickListener onClickListener) {
        if (onClickListener != null) {
            mymore_more.setOnClickListener(onClickListener);
        }
    }
}